//index.js
//获取应用实例
const app = getApp()
var replyAnimation;
var up;
var down;
var videopage = 1;
Page({
  data:{
    hideImg:false,
    swiperCurrent:0,
    curswiperIndex:0,
    isPlay:true,
    commentContent:'',//评论内容
    focusInput: false,
    isInput: false,
    inputTalk:'',
    showtools:false,
    videoList: [],//视屏列表
    talks:[],
    replys:[],
    curvideo: {},//当前视屏
    cururl:'',//当前视屏路径
    curcomment:{},//当前评论
    curCommentIndex:'',//当前评论的索引
    videoindex: 0,
    videoH: 600,//高度
    videoBtnStatus: 'none',//视屏按钮状态
    controls:false,
    tempUrl:'https://aisong-other-test.oss-cn-shenzhen.aliyuncs.com/upload/music-test/%E6%A1%94%E6%A2%97.jpg?OSSAccessKeyId=LTAIiuQUldDpX7ri&Expires=2573809587&Signature=MxjfvR2iWI%2FetWxB7yHMCWpN8ZI%3D'
  },
  totalPage:1,
  page: 1, //活动页码
  pageSize: 10, //每页显示数量
  noDataTip: '暂无数据,切换一个标签吧',
  isEnd: false,
  
  

 
  // openId: app.openId,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    videopage = 0;
    wx.getSystemInfo({//适配
      success: function (res) {
        console.info("版本号", +res.SDKVersion.replace(/\./g, ""));
        console.log(res)
        that.setData({
          videoH:res.windowHeight
        })
      }

    })
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    that.getVideoList();
  },

  //生命周期函数--监听页面初次渲染完成
  onReady:function(){
    var that = this;
    wx.setNavigationBarTitle({
      title: "抖音",
    })
  },
  onShow:function(){
    var that  = this;
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
 

  /**
   * 模拟获取视频列表
   */
   getVideoList: function (){
    var that = this
    that.setData({
      videoList:[{id:1,mvUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145009.mp4',imgUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145009.mp4?x-oss-process=video/snapshot,t_1300,m_fast'},
        {id:2,mvUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145032.mp4',imgUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145032.mp4?x-oss-process=video/snapshot,t_1300,m_fast'},
        {id:3,mvUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145037.mp4',imgUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145037.mp4?x-oss-process=video/snapshot,t_1300,m_fast'},
        {id:4,mvUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145046.mp4',imgUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145046.mp4?x-oss-process=video/snapshot,t_1300,m_fast'}],
      curvideo:{id:1,mvUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145009.mp4',imgUrl:'https://tomcat-a.oss-cn-beijing.aliyuncs.com/video/WeChat_20200121145009.mp4?x-oss-process=video/snapshot,t_1300,m_fast'}
    })
    wx.hideLoading();
    
  },
  
  //监听视频播放进度
  videoProgress: function (e) {
    var that = this;
    console.info(e.detail.duration, e.detail.currentTime);
    var duration = e.detail.duration;
    var currentTime = e.detail.currentTime;
    var progress = currentTime / duration * 100;
    that.setData({
      vProgress: progress
    })
  },

  startPlay:function(){
    var that = this
    if(that.data.isPlay==false){
      that.setData({
        isPlay:true
      })
    }
  },
  //加载视频
  loadVideo:function(){
    wx.hideLoading()
    if(this.data.videoindex == 0){
      wx.showToast({
        image:'../../images/upup.png',
        title:'上滑查看更多',
        duration:3000
      })
    }
    console.log("视频开始播放")
  },
  //******************************************************* */
  //改变轮播时
  changeVideo:function (e){
    console.log(e)
    var that  = this;
    that.setData({
      hideImg:true
    })
    if(e.detail.source == 'touch'){
      this.setData({
        swiperCurrent: e.detail.current //获取当前轮播图片的下标
      })
    }
    var curswiperIndex = e.detail.current
    //判断是否结束
    var count = that.data.videoList.length
    if(count==(curswiperIndex+1)){
      that.getVideoList(that.page,'')
    }
    var lastIndex = that.data.curswiperIndex
        //判断是上滑还是下滑
        that.setData({
        curswiperIndex:curswiperIndex
      })
      var videoindex = that.data.videoindex;
      if(curswiperIndex>lastIndex){
        //上滑
        videopage++
        videoindex = videopage
        var curvideo = that.data.videoList[videoindex].activity
        that.setData({
          videoindex:videoindex,
          curvideo:curvideo,
          videoBtnStatus:'none'
        })
        setTimeout(function(){
          that.setData({
            hideImg:false
          })
        },1200)
        //当视频有mvUrl时显示加载
        if(curvideo.mvUrl){
          wx.showLoading()
        }
       
   }else{
     //下滑
      videopage--
      videoindex = videopage
      var curvideo = that.data.videoList[videoindex].activity
      that.setData({
          videoindex:videoindex,
          curvideo:curvideo,
          videoBtnStatus:'none'
      })
      setTimeout(function(){
        that.setData({
          hideImg:false
        })
      },1200)
     //当视频有mvUrl时显示加载
     if(curvideo.mvUrl){
      wx.showLoading()
      }
    }
  },
  //点击视频暂停
  pauseVideo:function(){
    var that = this; 
    if(that.data.videoBtnStatus){
      wx.createVideoContext("video"+that.data.videoindex).pause()
      that.setData({
        videoBtnStatus:''
      })
    }
  },
  //点击播放按钮
  playVideo:function(){
    var that = this; 
    wx.createVideoContext("video"+that.data.videoindex).play()
    that.setData({
      videoBtnStatus:'none'
    })
  },
})
